﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using fideeraSoft.Handlers;
using fideeraSoft.Utils;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Drawing;

namespace fideeraSoft.Paneles
{
    public class ConsultaStockActualPanel:PanelGeneral
    {
        private System.Windows.Forms.DataGridView dgvMostrar;
        private System.Windows.Forms.TableLayoutPanel tlpBuscarLoteId;
        private System.Windows.Forms.TextBox tbBuscarLoteId;
        private System.Windows.Forms.TableLayoutPanel tlpBotones;
        private System.Windows.Forms.Button btVolver;
        private System.Windows.Forms.DataGridView dgvMostrar2;
        private System.Windows.Forms.Button btBuscarLoteId;

        private SqlDataAdapter adapter;
        private DateTimePicker dtpBuscarFin;
        private Button btBuscarEntreFecha;
        private DateTimePicker dtpBuscarIni;
        private TableLayoutPanel tlpBuscarEntreFechas;

        private DataSet ds;

        private int locationBuscarLoteId=0;
        private int locationBuscarEntreFechas = 0;
        private Button btExportExcel2;
        private Button btExportExcel;
        private TableLayoutPanel tlpExportExcel2;
        private TableLayoutPanel tlpExportExcel;
        int inPosicionDgvMatPrima = 0;
        int locationTablaExportExcel2 = 0;
        int locationDataGridView = 0;

        public ConsultaStockActualPanel(Handler handler)
        {
            this.formatearPanelGeneral(handler);

            try
            {
                InitializeComponent();
                this.formatearTableTitulo("CONSULTA DE STOCK ACTUAL POR LOTE");

                string query = "SELECT l.ID AS [ID Lote], l.codigo AS Código, l.fechaVencimiento AS [Fecha de Vencimiento Lote], l.fechaRecepcion AS [Fecha de Recepción Lote], re.ID AS [ID de Remito de Entrada]  FROM Lote AS l INNER JOIN RemitoEntrada_X_Lote AS rexl ON l.ID=rexl.lote INNER JOIN RemitoEntrada AS re ON rexl.remitoEntrada=re.ID ";  
                
                adapter = SQLclass.obtenerAdapter(query);
                if (adapter != null)
                {
                    ds = new DataSet();
                    adapter.Fill(ds);

                    if (ds.Tables[0].Rows.Count <= 0)
                    {
                        MessageBox.Show("NO HAY NINGUN LOTE CARGADO", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }else
                    {
                        dgvMostrar.DataSource = ds.Tables[0];
                        if (dgvMostrar.CurrentCell != null)
                        {
                           
                            dgvMostrar.CurrentCell = dgvMostrar.Rows[0].Cells[0];
                            cargarMatPrimaEnDataGridView(dgvMostrar[0, dgvMostrar.CurrentCell.RowIndex].Value.ToString());
                        }
                        else 
                        {
                            
                        }
                    }

                    formatearTablaExportExcel();

                    formatearDataGrid();

                    formatearTablaBuscarLoteId();

                    formatearTablaBuscarEntreFechas();

                    formatearDataGridMatPrima();
                    
                    formatearTablaExportExcel2();

                    formatearTablaBotones();

                    this.Controls.Add(tlpExportExcel);
                    this.Controls.Add(dgvMostrar);
                    
                    this.Controls.Add(tlpBuscarLoteId);

                    this.Controls.Add(tlpBuscarEntreFechas);

                    this.Controls.Add(dgvMostrar2);
                    
                    this.Controls.Add(tlpExportExcel2);
                    
                    this.Controls.Add(tlpBotones);

                }

                else
                {

                    MessageBox.Show("NO HAY NINGUN LOTE CARGADO", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.handler.backToPrincipal();
                }

            }
            catch (MiException ex)
            {

                CLogger.Append("[ConsultaStockActualPanel] Exception: " + ex, CLogger.ERROR);      //DEBUG

            }
        }

        #region FORMATEO

        private void formatearTablaExportExcel()
        {
            tlpExportExcel.Controls.Add(btExportExcel);

            // int wid = MetodosGenerales.calcularCenterWidth(handler.FormFrame.Size.Width, tlpExportExcel.Size.Width);

            tlpExportExcel.Location = new System.Drawing.Point(50, this.InitialRealPointVertical);
        }

        private void formatearDataGrid()
        {
            locationDataGridView = this.InitialRealPointVertical + tlpExportExcel.Size.Height + 10;

            dgvMostrar.AutoGenerateColumns = true;
            dgvMostrar.Size = new Size(this.Size.Width - 100, 300);
            dgvMostrar.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.Fill);
            dgvMostrar.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllHeaders);
            dgvMostrar.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

            dgvMostrar.BorderStyle = BorderStyle.Fixed3D;

            dgvMostrar.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, dgvMostrar.Size.Width), locationDataGridView);
        }

        private void formatearTablaBuscarLoteId()
        {
            tlpBuscarLoteId.Controls.Add(btBuscarLoteId);
            tlpBuscarLoteId.Controls.Add(tbBuscarLoteId);

            //tlpBuscarProv.Size = new Size(lbBuscarProvCuil.Size.Width + tbBuscarProvCuil.Size.Width + 10,tbBuscarProvCuil.Size.Height);
            locationBuscarLoteId = this.locationDataGridView + dgvMostrar.Size.Height + 15;

            int wid = MetodosGenerales.calcularCenterWidth(handler.FormFrame.Size.Width, tlpBuscarLoteId.Size.Width);            

            tlpBuscarLoteId.Location = new Point(wid, locationBuscarLoteId);


        }


        private void formatearTablaBuscarEntreFechas()
        {
            //tlpBuscarEntreFechas.Size = new Size(400, tbBuscarRemitoCod.Size.Height + 9);
            locationBuscarEntreFechas = locationBuscarLoteId + tlpBuscarLoteId.Size.Height + 5;

            tlpBuscarEntreFechas.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, tlpBuscarEntreFechas.Size.Width), locationBuscarEntreFechas);

            tlpBuscarEntreFechas.Controls.Add(btBuscarEntreFecha);

            dtpBuscarIni.Value = System.DateTime.Now;
            dtpBuscarFin.Value = System.DateTime.Now;

            tlpBuscarEntreFechas.Controls.Add(dtpBuscarIni);
            tlpBuscarEntreFechas.Controls.Add(dtpBuscarFin);


        }

        private void formatearDataGridMatPrima()
        {            
            inPosicionDgvMatPrima = locationBuscarEntreFechas + tlpBuscarEntreFechas.Size.Height + 15;

            dgvMostrar2.AutoGenerateColumns = true;
            dgvMostrar2.Size = new Size(this.Size.Width - 100, 160);
            dgvMostrar2.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.Fill);
            dgvMostrar2.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllHeaders);
            dgvMostrar2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

            dgvMostrar2.BorderStyle = BorderStyle.Fixed3D;

            dgvMostrar2.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, dgvMostrar2.Size.Width), this.inPosicionDgvMatPrima);
        }

        private void formatearTablaExportExcel2()
        {
            tlpExportExcel2.Controls.Add(btExportExcel2);

            locationTablaExportExcel2 = inPosicionDgvMatPrima + dgvMostrar2.Size.Height + 10;

            //int wid = MetodosGenerales.calcularCenterWidth(handler.FormFrame.Size.Width, tlpExportExcel2.Size.Width);

            tlpExportExcel2.Location = new System.Drawing.Point(50, locationTablaExportExcel2);
        }

        private void formatearTablaBotones()
        {
            int locationBtCambios = locationTablaExportExcel2 + tlpExportExcel2.Size.Height + 15;
            tlpBotones.Location = new Point(MetodosGenerales.calcularCenterWidth(this.handler.FormFrame.Size.Width, tlpBotones.Size.Width), locationBtCambios);

            tlpBotones.Controls.Add(btVolver);


        }


        #endregion

        #region INICIALIZAR COMPONETES
        private void InitializeComponent()
        {
            this.dgvMostrar = new System.Windows.Forms.DataGridView();
            this.tlpBuscarLoteId = new System.Windows.Forms.TableLayoutPanel();
            this.tbBuscarLoteId = new System.Windows.Forms.TextBox();
            this.tlpBotones = new System.Windows.Forms.TableLayoutPanel();
            this.btVolver = new System.Windows.Forms.Button();
            this.btBuscarLoteId = new System.Windows.Forms.Button();
            this.dgvMostrar2 = new System.Windows.Forms.DataGridView();
            this.dtpBuscarFin = new System.Windows.Forms.DateTimePicker();
            this.btBuscarEntreFecha = new System.Windows.Forms.Button();
            this.dtpBuscarIni = new System.Windows.Forms.DateTimePicker();
            this.tlpBuscarEntreFechas = new System.Windows.Forms.TableLayoutPanel();
            this.btExportExcel2 = new System.Windows.Forms.Button();
            this.btExportExcel = new System.Windows.Forms.Button();
            this.tlpExportExcel2 = new System.Windows.Forms.TableLayoutPanel();
            this.tlpExportExcel = new System.Windows.Forms.TableLayoutPanel();
            ((System.ComponentModel.ISupportInitialize)(this.dgvMostrar)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.dgvMostrar2)).BeginInit();
            this.SuspendLayout();
            // 
            // dgvMostrar
            // 
            this.dgvMostrar.AllowUserToResizeRows = false;
            this.dgvMostrar.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dgvMostrar.EditMode = System.Windows.Forms.DataGridViewEditMode.EditOnF2;
            this.dgvMostrar.Location = new System.Drawing.Point(0, 0);
            this.dgvMostrar.Name = "dgvMostrar";
            this.dgvMostrar.ReadOnly = true;
            this.dgvMostrar.RowTemplate.DefaultCellStyle.Font = new System.Drawing.Font("Bodoni MT", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dgvMostrar.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.CellSelect;
            this.dgvMostrar.Size = new System.Drawing.Size(240, 150);
            this.dgvMostrar.TabIndex = 0;
            this.dgvMostrar.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvMostrarLotes_CellClick);
            this.dgvMostrar.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvMostrarLotes_CellContentClick);
            // 
            // tlpBuscarLoteId
            // 
            this.tlpBuscarLoteId.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBuscarLoteId.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBuscarLoteId.ColumnCount = 2;
            this.tlpBuscarLoteId.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarLoteId.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarLoteId.Location = new System.Drawing.Point(0, 0);
            this.tlpBuscarLoteId.Name = "tlpBuscarLoteId";
            this.tlpBuscarLoteId.RowCount = 1;
            this.tlpBuscarLoteId.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarLoteId.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarLoteId.Size = new System.Drawing.Size(300, 40);
            this.tlpBuscarLoteId.TabIndex = 0;
            // 
            // tbBuscarLoteId
            // 
            this.tbBuscarLoteId.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
            this.tbBuscarLoteId.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbBuscarLoteId.Location = new System.Drawing.Point(0, 0);
            this.tbBuscarLoteId.MaxLength = 8;
            this.tbBuscarLoteId.Name = "tbBuscarLoteId";
            this.tbBuscarLoteId.Size = new System.Drawing.Size(100, 30);
            this.tbBuscarLoteId.TabIndex = 0;
            // 
            // tlpBotones
            // 
            this.tlpBotones.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBotones.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBotones.ColumnCount = 1;
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpBotones.Location = new System.Drawing.Point(0, 0);
            this.tlpBotones.Name = "tlpBotones";
            this.tlpBotones.RowCount = 1;
            this.tlpBotones.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpBotones.Size = new System.Drawing.Size(100, 46);
            this.tlpBotones.TabIndex = 0;
            // 
            // btVolver
            // 
            this.btVolver.BackColor = System.Drawing.Color.AliceBlue;
            this.btVolver.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btVolver.Location = new System.Drawing.Point(0, 0);
            this.btVolver.Margin = new System.Windows.Forms.Padding(12, 3, 3, 3);
            this.btVolver.Name = "btVolver";
            this.btVolver.Size = new System.Drawing.Size(75, 40);
            this.btVolver.TabIndex = 0;
            this.btVolver.Text = "Volver";
            this.btVolver.UseVisualStyleBackColor = false;
            this.btVolver.Click += new System.EventHandler(this.btVolver_Click);
            // 
            // btBuscarLoteId
            // 
            this.btBuscarLoteId.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.btBuscarLoteId.BackColor = System.Drawing.Color.AliceBlue;
            this.btBuscarLoteId.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btBuscarLoteId.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btBuscarLoteId.Location = new System.Drawing.Point(0, 0);
            this.btBuscarLoteId.Name = "btBuscarLoteId";
            this.btBuscarLoteId.Size = new System.Drawing.Size(75, 23);
            this.btBuscarLoteId.TabIndex = 0;
            this.btBuscarLoteId.Text = "Buscar Lote (ID)";
            this.btBuscarLoteId.UseVisualStyleBackColor = false;
            this.btBuscarLoteId.Click += new System.EventHandler(this.btBuscarLoteId_Click);
            // 
            // dgvMostrar2
            // 
            this.dgvMostrar2.AllowUserToResizeRows = false;
            this.dgvMostrar2.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dgvMostrar2.EditMode = System.Windows.Forms.DataGridViewEditMode.EditOnF2;
            this.dgvMostrar2.Location = new System.Drawing.Point(0, 0);
            this.dgvMostrar2.Name = "dgvMostrar2";
            this.dgvMostrar2.ReadOnly = true;
            this.dgvMostrar2.RowTemplate.DefaultCellStyle.Font = new System.Drawing.Font("Bodoni MT", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dgvMostrar2.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.CellSelect;
            this.dgvMostrar2.Size = new System.Drawing.Size(240, 150);
            this.dgvMostrar2.TabIndex = 0;
            this.dgvMostrar2.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvMostrarMatPrima_CellClick);
            this.dgvMostrar2.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvMostrarMatPrima_CellContentClick);
            // 
            // dtpBuscarFin
            // 
            this.dtpBuscarFin.CalendarFont = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dtpBuscarFin.Font = new System.Drawing.Font("Bodoni MT", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dtpBuscarFin.Location = new System.Drawing.Point(0, 0);
            this.dtpBuscarFin.Margin = new System.Windows.Forms.Padding(3, 7, 3, 3);
            this.dtpBuscarFin.Name = "dtpBuscarFin";
            this.dtpBuscarFin.Size = new System.Drawing.Size(300, 23);
            this.dtpBuscarFin.TabIndex = 0;
            // 
            // btBuscarEntreFecha
            // 
            this.btBuscarEntreFecha.BackColor = System.Drawing.Color.AliceBlue;
            this.btBuscarEntreFecha.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btBuscarEntreFecha.Font = new System.Drawing.Font("Bodoni MT", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btBuscarEntreFecha.Location = new System.Drawing.Point(0, 0);
            this.btBuscarEntreFecha.Name = "btBuscarEntreFecha";
            this.btBuscarEntreFecha.Size = new System.Drawing.Size(75, 23);
            this.btBuscarEntreFecha.TabIndex = 0;
            this.btBuscarEntreFecha.Text = "Buscar entre fechas";
            this.btBuscarEntreFecha.UseVisualStyleBackColor = false;
            this.btBuscarEntreFecha.Click += new System.EventHandler(this.btBuscarEntreFecha_Click);
            // 
            // dtpBuscarIni
            // 
            this.dtpBuscarIni.CalendarFont = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dtpBuscarIni.Font = new System.Drawing.Font("Bodoni MT", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dtpBuscarIni.Location = new System.Drawing.Point(0, 0);
            this.dtpBuscarIni.Margin = new System.Windows.Forms.Padding(3, 7, 3, 3);
            this.dtpBuscarIni.Name = "dtpBuscarIni";
            this.dtpBuscarIni.Size = new System.Drawing.Size(300, 23);
            this.dtpBuscarIni.TabIndex = 0;
            // 
            // tlpBuscarEntreFechas
            // 
            this.tlpBuscarEntreFechas.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBuscarEntreFechas.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBuscarEntreFechas.ColumnCount = 3;
            this.tlpBuscarEntreFechas.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
            this.tlpBuscarEntreFechas.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 37.5F));
            this.tlpBuscarEntreFechas.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 37.5F));
            this.tlpBuscarEntreFechas.Location = new System.Drawing.Point(0, 0);
            this.tlpBuscarEntreFechas.Name = "tlpBuscarEntreFechas";
            this.tlpBuscarEntreFechas.RowCount = 1;
            this.tlpBuscarEntreFechas.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpBuscarEntreFechas.Size = new System.Drawing.Size(600, 40);
            this.tlpBuscarEntreFechas.TabIndex = 0;
            // 
            // btExportExcel2
            // 
            this.btExportExcel2.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.btExportExcel2.BackColor = System.Drawing.Color.AliceBlue;
            this.btExportExcel2.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btExportExcel2.Font = new System.Drawing.Font("Bodoni MT", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btExportExcel2.Location = new System.Drawing.Point(0, 0);
            this.btExportExcel2.Name = "btExportExcel2";
            this.btExportExcel2.Size = new System.Drawing.Size(75, 23);
            this.btExportExcel2.TabIndex = 0;
            this.btExportExcel2.Text = "Exportar";
            this.btExportExcel2.UseVisualStyleBackColor = false;
            this.btExportExcel2.Click += new System.EventHandler(this.btExportExcel2_Click);
            // 
            // btExportExcel
            // 
            this.btExportExcel.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.btExportExcel.BackColor = System.Drawing.Color.AliceBlue;
            this.btExportExcel.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btExportExcel.Font = new System.Drawing.Font("Bodoni MT", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btExportExcel.Location = new System.Drawing.Point(0, 0);
            this.btExportExcel.Name = "btExportExcel";
            this.btExportExcel.Size = new System.Drawing.Size(75, 23);
            this.btExportExcel.TabIndex = 0;
            this.btExportExcel.Text = "Exportar";
            this.btExportExcel.UseVisualStyleBackColor = false;
            this.btExportExcel.Click += new System.EventHandler(this.btExportExcel_Click);
            // 
            // tlpExportExcel2
            // 
            this.tlpExportExcel2.BackColor = System.Drawing.Color.Transparent;
            this.tlpExportExcel2.ColumnCount = 1;
            this.tlpExportExcel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpExportExcel2.Location = new System.Drawing.Point(0, 0);
            this.tlpExportExcel2.Name = "tlpExportExcel2";
            this.tlpExportExcel2.RowCount = 1;
            this.tlpExportExcel2.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpExportExcel2.Size = new System.Drawing.Size(100, 35);
            this.tlpExportExcel2.TabIndex = 0;
            // 
            // tlpExportExcel
            // 
            this.tlpExportExcel.BackColor = System.Drawing.Color.Transparent;
            this.tlpExportExcel.ColumnCount = 1;
            this.tlpExportExcel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpExportExcel.Location = new System.Drawing.Point(0, 0);
            this.tlpExportExcel.Name = "tlpExportExcel";
            this.tlpExportExcel.RowCount = 1;
            this.tlpExportExcel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpExportExcel.Size = new System.Drawing.Size(100, 35);
            this.tlpExportExcel.TabIndex = 0;
            ((System.ComponentModel.ISupportInitialize)(this.dgvMostrar)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.dgvMostrar2)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion 

        private void cargarMatPrimaEnDataGridView(string idLote)
        {
            try
            {
                if (dgvMostrar.CurrentCell == null)
                {
                    return;
                }

                dgvMostrar2.DataSource = null;
                dgvMostrar2.Rows.Clear();

                string query = "SELECT mp.ID AS [ID Materia Prima], mp.nombre AS Nombre, mp.codigo AS Código, mp.descripcion AS Descripción, mpl.cantidad AS Cantidad, mp.unidad AS Unidad FROM MateriaPrima AS mp INNER JOIN MateriaPrima_X_Lote AS mpl ON mp.ID=mpl.materiaPrima AND mpl.lote='"+idLote+"'";

                adapter = SQLclass.obtenerAdapter(query);
                if (adapter != null)
                {
                    ds = new DataSet();
                    adapter.Fill(ds);

                    if (ds.Tables[0].Rows.Count <= 0)
                    {
                        MessageBox.Show("NO HAY NINGUNA MATERIA PRIMA CARGADA PARA ESE LOTE", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    else
                    {
                        dgvMostrar2.DataSource = ds.Tables[0];
                        
                    }
                }

                else
                {

                    MessageBox.Show("NO HAY NINGUNA MATERIA PRIMA CARGADA PARA ESE LOTE", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    //this.handler.backToPrincipal();
                }

            }
            catch (MiException ex)
            {

                CLogger.Append("[cargarMatPrimaEnDataGridView] Exception: " + ex, CLogger.ERROR);      //DEBUG

            }
        }

        #region BOTONES
        private void btVolver_Click(object sender, EventArgs e)
        {
            this.handler.backToPrincipal();
        }

        private void btBuscarLoteId_Click(object sender, EventArgs e)
        {
            if (dgvMostrar.Rows.Count <= 0)
                return;

            dgvMostrar.ClearSelection();
            if (!string.IsNullOrWhiteSpace(tbBuscarLoteId.Text))
            {
                string searchValue = tbBuscarLoteId.Text;
                try
                {
                    foreach (DataGridViewRow row in dgvMostrar.Rows)
                    {
                        if (row.Cells[0].Value.ToString().Equals(searchValue))
                        {
                            dgvMostrar.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                            row.Selected = true;

                            //MessageBox.Show("ENCONTRE: "+inRowFinded, "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            tbBuscarLoteId.Clear();

                            dgvMostrar.CurrentCell = row.Cells[0];
                            //dgvMostrarLotes.FirstDisplayedScrollingRowIndex=row.Index;

                            if (dgvMostrar.CurrentCell != null)
                            {
                                cargarMatPrimaEnDataGridView(dgvMostrar[0, dgvMostrar.CurrentCell.RowIndex].Value.ToString());
                                
                            }
                            break;

                        }

                    }

                }
                catch (Exception exc)
                {
                    tbBuscarLoteId.Clear();
                    MessageBox.Show("NO SE ENCONTRO EL LOTE BUSCADO", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    // CLogger.Append("[btBuscarProducto_Click] Exception: " + exc, CLogger.ERROR);      //DEBUG
                }
            }
            else
            {
                MessageBox.Show("NO SE INGRESO LOTE A BUSCAR", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

            }
        }

        private void btBuscarEntreFecha_Click(object sender, EventArgs e)
        {
            if (dgvMostrar.CurrentCell == null)
            {
                MessageBox.Show("NO HAY NINGUN VALOR CARGADO EN LA TABLA", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            dgvMostrar.ClearSelection();
            if ((dtpBuscarIni.Value == null) || (dtpBuscarFin.Value == null))
            {
                MessageBox.Show("VALORES DE FECHA ERRONEOS", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            //if (dtpBuscarIni.Value > dtpBuscarFin.Value) 
            if (DateTime.Compare(dtpBuscarIni.Value, dtpBuscarFin.Value) > 0)
            {
                MessageBox.Show("LA FECHA PRIMARIA NO PUEDE SER MAYOR QUE LA FECHA SECUNDARIA", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            //MessageBox.Show("FECHA 1: " + dtBuscarIni.Value.ToString("MM/dd/yyyy") + " FECHA 2: " + dtBuscarFin.Value.ToString("MM/dd/yyyy"), "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

            string query = "SELECT ID AS idLote, codigo AS Código, fechaVencimiento AS FechaVencimiento, fechaRecepcion AS FechaRecepción FROM Lote WHERE (fechaRecepcion BETWEEN @FECHAINICIO AND @FECHAFIN)";
            
            adapter = SQLclass.obtenerAdapter(query);

            adapter.SelectCommand.Parameters.AddWithValue("@FECHAINICIO", dtpBuscarIni.Value);
            //adapter.SelectCommand.Parameters.AddWithValue("@FECHAFIN", dtpBuscarFin.Value.ToString("MM/dd/yyyy") + " 23:59");

            adapter.SelectCommand.Parameters.AddWithValue("@FECHAFIN", dtpBuscarFin.Value);

            if (adapter != null)
            {
                ds = new DataSet();
                adapter.Fill(ds);

                if (ds.Tables[0].Rows.Count <= 0)
                {
                    MessageBox.Show("NO SE ENCONTRARON LOTES ENTRE ESAS FECHAS", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                }
                else
                {
                    dgvMostrar2.DataSource = "";
                    dgvMostrar2.Refresh();

                    dgvMostrar.DataSource = ds.Tables[0];

                }


            }
            else
            {
                //CLogger.Append("[ProveedorPanelMostrar] Lista Proveedor mostrar Null: ", CLogger.INFO);      //DEBUG
                MessageBox.Show("ERROR. NO SE ENCONTRARON LOTES ENTRE ESAS FECHAS", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.handler.backToPrincipal();
            }
        }

        private void dgvMostrarLotes_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            dgvMostrar.SelectionMode = DataGridViewSelectionMode.CellSelect;
            dgvMostrar.CurrentCell = dgvMostrar.Rows[dgvMostrar.CurrentCell.RowIndex].Cells[0];

            if (dgvMostrar.CurrentCell != null)
            {
                cargarMatPrimaEnDataGridView(dgvMostrar[0,dgvMostrar.CurrentCell.RowIndex].Value.ToString());
            }
        }

        private void dgvMostrarLotes_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            dgvMostrar.SelectionMode = DataGridViewSelectionMode.CellSelect;
            dgvMostrar.CurrentCell = dgvMostrar.Rows[dgvMostrar.CurrentCell.RowIndex].Cells[0];

            if (dgvMostrar.CurrentCell != null)
                cargarMatPrimaEnDataGridView(dgvMostrar[0, dgvMostrar.CurrentCell.RowIndex].Value.ToString());
        }

        private void dgvMostrarMatPrima_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            
            dgvMostrar2.SelectionMode = DataGridViewSelectionMode.CellSelect;
            dgvMostrar2.CurrentCell = dgvMostrar2.Rows[dgvMostrar2.CurrentCell.RowIndex].Cells[0];
        }

        private void dgvMostrarMatPrima_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            dgvMostrar2.SelectionMode = DataGridViewSelectionMode.CellSelect;
            dgvMostrar2.CurrentCell = dgvMostrar2.Rows[dgvMostrar2.CurrentCell.RowIndex].Cells[0];
        }

        #endregion

        #region EXCEL

        private void btExportExcel_Click(object sender, EventArgs e)
        {
            try
            {
                if (dgvMostrar.CurrentCell == null)
                {
                    MessageBox.Show("NO HAY DATOS EN LA TABLA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    //MessageBox.Show("NO HAY CELDA SELECCIONADA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                dgvMostrar.SelectionMode = DataGridViewSelectionMode.CellSelect;
                dgvMostrar.CurrentCell = dgvMostrar.Rows[dgvMostrar.CurrentCell.RowIndex].Cells[0];

                copyAlltoClipboard();
                Microsoft.Office.Interop.Excel.Application xlexcel;
                Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
                Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;

                object misValue = System.Reflection.Missing.Value;
                xlexcel = new Microsoft.Office.Interop.Excel.Application();

                xlexcel.Visible = true;

                xlWorkBook = xlexcel.Workbooks.Add(misValue);

                xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                xlWorkSheet.Name = "Stock Actual Lotes";

                Microsoft.Office.Interop.Excel.Range CR = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[2, 1];
                CR.Select();
                xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);

                formatearExcel1(xlWorkSheet);


            }
            catch (Exception ex)
            {
                CLogger.Append("[btExportExcel_Click] Exception  export: " + ex, CLogger.INFO);      //DEBUG
            }
        
        }

        private static void formatearExcel1(Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet)
        {
            
            xlWorkSheet.Cells[1, 1] = "ID Lote";
            xlWorkSheet.Cells[1, 2] = "Código";
            xlWorkSheet.Cells[1, 3] = "Fecha de Vencimiento";
            xlWorkSheet.Cells[1, 4] = "Fecha de Recepción";
            xlWorkSheet.Cells[1, 5] = "ID Remito de Entrada";

            xlWorkSheet.Cells[1, 1].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
            xlWorkSheet.Cells[1, 1].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);

            xlWorkSheet.Cells[1, 2].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
            xlWorkSheet.Cells[1, 2].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);

            xlWorkSheet.Cells[1, 3].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
            xlWorkSheet.Cells[1, 3].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);

            xlWorkSheet.Cells[1, 4].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
            xlWorkSheet.Cells[1, 4].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);

            xlWorkSheet.Cells[1, 5].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
            xlWorkSheet.Cells[1, 5].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);

            xlWorkSheet.Columns.AutoFit();
        }

        private void copyAlltoClipboard()
        {
            dgvMostrar.SelectAll();
            DataObject dataObj = dgvMostrar.GetClipboardContent();
            if (dataObj != null)
                Clipboard.SetDataObject(dataObj);
        }


        private void btExportExcel2_Click(object sender, EventArgs e)
        {
            try
            {
                if (dgvMostrar2.CurrentCell == null)
                {
                    MessageBox.Show("NO HAY DATOS EN LA TABLA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                dgvMostrar2.SelectionMode = DataGridViewSelectionMode.CellSelect;
                dgvMostrar2.CurrentCell = dgvMostrar2.Rows[dgvMostrar2.CurrentCell.RowIndex].Cells[0];

                copyAlltoClipboard2();
                Microsoft.Office.Interop.Excel.Application xlexcel;
                Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
                Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;

                object misValue = System.Reflection.Missing.Value;
                xlexcel = new Microsoft.Office.Interop.Excel.Application();

                xlexcel.Visible = true;

                xlWorkBook = xlexcel.Workbooks.Add(misValue);

                xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                xlWorkSheet.Name = "MateriaPrimaPorLote";

                Microsoft.Office.Interop.Excel.Range CR = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[2, 2];
                CR.Select();
                xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);

                formatearExcel2(xlWorkSheet);

            }
            catch (Exception ex)
            {
                CLogger.Append("[btExportExcel2_Click] Exception  export: " + ex, CLogger.INFO);      //DEBUG
            }
        }

        private void formatearExcel2(Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet)
        {   

            xlWorkSheet.Cells[1, 2] = "ID Materia Prima";
            xlWorkSheet.Cells[1, 3] = "Nombre";
            xlWorkSheet.Cells[1, 4] = "Código";
            xlWorkSheet.Cells[1, 5] = "Descripción";
            xlWorkSheet.Cells[1, 6] = "Cantidad";
            xlWorkSheet.Cells[1, 7] = "Unidad";

            xlWorkSheet.Cells[1, 1] = "ID Lote";

            xlWorkSheet.Cells[2, 1] = dgvMostrar[0, dgvMostrar.CurrentCell.RowIndex].Value.ToString();

            xlWorkSheet.Cells[1, 1].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
            xlWorkSheet.Cells[1, 1].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);

            xlWorkSheet.Cells[1, 2].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
            xlWorkSheet.Cells[1, 2].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);

            xlWorkSheet.Cells[1, 3].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
            xlWorkSheet.Cells[1, 3].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);

            xlWorkSheet.Cells[1, 4].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
            xlWorkSheet.Cells[1, 4].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);

            xlWorkSheet.Cells[1, 5].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
            xlWorkSheet.Cells[1, 5].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);

            xlWorkSheet.Cells[1, 6].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
            xlWorkSheet.Cells[1, 6].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);

            xlWorkSheet.Cells[1, 7].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
            xlWorkSheet.Cells[1, 7].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);

            xlWorkSheet.Columns.AutoFit();
        }

        private void copyAlltoClipboard2()
        {
            dgvMostrar2.SelectAll();
            DataObject dataObj = dgvMostrar2.GetClipboardContent();
            if (dataObj != null)
                Clipboard.SetDataObject(dataObj);
        }

        #endregion
    }


}
